package com.huonan.jb.tools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @Description
 * @Author lxp
 * @Date 2023/3/17 下午4:46
 * @Version V1.0
 **/
public class FieldCopy {

    private static final String kwValue = "kw_value";
    private List<Map<String, String>> collect;

    public static void main(String[] args) {

        String path = "/Users/lxp/IdeaProjects/Study/MainTest/data/rank_douyin_20240315.json";
        String fPath = "/Users/lxp/IdeaProjects/Study/MainTest/data/heima.txt";
        String oPath = "/Users/lxp/IdeaProjects/Study/MainTest/data/outHeima.txt";
        List<String> fields = FileUtil.readUtf8Lines(fPath);
        List<String> list = FileUtil.readUtf8Lines(path);
        StringJoiner csvJ = new StringJoiner("\n");
        for (String l : list) {
            JSONObject obj = JSONUtil.parseObj(l);
            StringJoiner joiner = new StringJoiner(",");
            for (String key : fields) {
                Object value = obj.get(key);
                if (ObjectUtil.isNotEmpty(value)) {
                    joiner.add(value.toString());
                }
            }
            csvJ.add(joiner.toString());
        }
        StringJoiner joiner = new StringJoiner(",");
        for (String field : fields) {
            joiner.add(field);
        }
        FileUtil.appendUtf8String(joiner+"\n",oPath);
        FileUtil.appendUtf8String(csvJ.toString(),oPath);
    }
}
